package com.czf.easyexcel.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.czf.easyexcel.dao.BatchMapper;
import com.czf.easyexcel.entity.Student;
import com.czf.easyexcel.entity.Teacher;
import com.czf.easyexcel.listener.customizeListenner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

import static com.czf.easyexcel.entity.Student.initData;

@RestController
@Slf4j
@Transactional
public class StudentController {
    @Autowired
    private BatchMapper batchMapper;
    @RequestMapping("/read")
    public String readExcel(MultipartFile uploadExcel){
        try {
            ExcelReader excelReader = EasyExcel.read(uploadExcel.getInputStream()).build();
            ReadSheet sheet0 = EasyExcel.readSheet("student").head(Student.class).registerReadListener(new customizeListenner<Student>(batchMapper)).build();
            ReadSheet sheet1 = EasyExcel.readSheet(1).head(Teacher.class).registerReadListener(new customizeListenner<Teacher>(batchMapper)).build();
            excelReader.read(sheet0, sheet1);
            // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
            excelReader.finish();
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }

    }


    /**
     * 文件下载
     * 1. 编写实体类并创建对象以便写入表格
     * 2. 设置响应参数：文件的ContentType和文件名，同时设置编码避免乱码
     * 3. 直接写，内部会调用finish方法自动关闭OutputStream
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        String fileName = URLEncoder.encode("多sheet", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");
        //工作簿对象
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        //sheet对象
        WriteSheet sheet0 = EasyExcel.writerSheet(0,"student").head(Student.class).build();
        WriteSheet sheet1 = EasyExcel.writerSheet(1,"teacher").head(Teacher.class).build();

        //写出
        excelWriter.write(batchMapper.findStudents(),sheet0);
        excelWriter.write(batchMapper.findTeachers(),sheet1);
        //关闭流
        excelWriter.finish();
    }


    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}
